<template>
  <div class="home">
    <div class="data-show">
      <div class="title">
        数据概况
      </div>
      <div class="data-body">
        <div class="item" v-for="item in allDataList">
          <div class="t">{{ item.statisticsName }}</div>
          <div class="b">
            <div class="count"> {{ item.count }}</div>
            <div>
              昨日新增:
              <span class="pre-count">{{ item.preCount }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="echarts">
      <div class="app" ref="allAppWeekDataRef">
      </div>
      <div class="content" ref="allContentWeekDataRef">
      </div>
    </div>
  </div>
</template>


<script setup>
import * as echarts from 'echarts';
import {onMounted, ref, getCurrentInstance, nextTick} from "vue";

const {proxy} = getCurrentInstance();

const api = {
  getContentWeekData: "/index/getContentWeekData", //获取近一周内容统计
  getAppWeekData: "/index/getAppWeekData", //获取app一周数据
  getAllData: "/index/getAllData", //获取近一周内容发布数据概括
}

// 查询数据
const allDataList = ref([])
const loadAllData = async () => {
  let result = await proxy.Request({
    url: api.getAllData
  })
  if (!result) {
    return;
  }
  allDataList.value = result.data;
}

// echarts 配置
const getOption = (xAxisData, legendData, seriesData) => {
  let option = {
    xAxis: {
      type: 'category',
      data: xAxisData
    },
    yAxis: {
      type: 'value'
    },
    legend: {
      data: legendData
    },
    series: seriesData
  };
  return option;
}

//获取近一周内容统计
const allContentWeekDataRef = ref();
const loadAllContentWeekData = async () => {
  let result = await proxy.Request({
    url: api.getContentWeekData,
  })
  if (!result) {
    return;
  }
  let data = result.data;
  let xAxisData = data.dateList.map((item) => item.slice(5));
  let legendData = data.itemDataList.map((item) => item.statisticsName);
  let seriesData = data.itemDataList.map((item) => {
    return {
      name: item.statisticsName,
      type: 'bar',
      emphasis: {
        focus: 'series'
      },
      data: item.listData
    }
  })
  let allContentWeekDataChart = echarts.init(allContentWeekDataRef.value);
  allContentWeekDataChart.setOption(getOption(xAxisData, legendData, seriesData));
}

//获取近一周app统计
const allAppWeekDataRef = ref();
const loadAllAppWeekData = async () => {
  let result = await proxy.Request({
    url: api.getAppWeekData,
  })
  if (!result) {
    return;
  }
  console.log(result)
  let data = result.data;
  let xAxisData = data.dateList.map((item) => item.slice(5));
  let legendData = data.itemDataList.map((item) => item.statisticsName);
  let seriesData = data.itemDataList.map((item) => {
    return {
      name: item.statisticsName,
      type: 'bar',
      emphasis: {
        focus: 'series'
      },
      data: item.listData
    }
  })
  let allAppWeekDataChart = echarts.init(allAppWeekDataRef.value);
  allAppWeekDataChart.setOption(getOption(xAxisData, legendData, seriesData));
}


onMounted(() => {
  loadAllData();
  loadAllContentWeekData();
  loadAllAppWeekData();
})
</script>


<style scoped lang="scss">
.home {
  //padding: 10px;

  .data-show {
    padding: 10px;
    background: #fff;
    box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
    border-radius: 5px;
    .title {
      color: #000;
      font-weight: bold;
      font-size: 18px;
    }

    .data-body {
      display: flex;
      font-size: 12px;

      .item {
        flex: 1;
        margin: 5px;
        padding: 10px;
        background: #F5F9FC;
        border-radius: 5px;

        .b {
          display: flex;
          justify-content: space-between;
          margin: 10px 0;
          align-items: center;

          .count {
            color: #000;
            font-weight: bold;
            font-size: 18px;
          }

          .pre-count {
            color: rgba(255, 0, 0, 0.88);
          }
        }
      }
    }
  }

  .echarts {
    margin-top: 10px;
    background: #fff;
    padding: 10px;
    display: flex;
    box-shadow:0px 0px 12px rgba(0, 0, 0, 0.12);
    border-radius: 5px;
    .app {
      height: calc(100vh - 285px);
      width: calc(100vh - 220px);

    }

    .content {
      height: calc(100vh - 285px);
      width: calc(100vh - 220px);
    }
  }
}
</style>
